//
//  DHRoutineController.m
//  DummyHTTPS
//
//  Created by Mike on 1/21/07.
//

#import "DHRoutineController.h"
#import "DHRoutine.h"

@interface DHRoutineController (Private)

- (void)setUtil:(DHRoutine *)newInstance;

@end

@implementation DHRoutineController (Private)

- (void)setUtil:(DHRoutine *)newInstance
{
	id oldInstance = nil;
	if (newInstance != oldInstance) {
		oldInstance = urlUtil;
		urlUtil = [newInstance retain];;
		[oldInstance autorelease];
	}
}

@end

@implementation DHRoutineController

- init
{
    if ((self = [super init])) {
		[self setUtil:[[DHRoutine alloc] init]];
    }
    return self;
}

- (void)dealloc {
    [self setUtil:nil];
    [super dealloc];
}

- (void)displayAlertWithMessageText:(NSString *)messageText
{
	NSAlert *alert = [[[NSAlert alloc] init] autorelease];
	[alert addButtonWithTitle:@"Ok"];
	[alert setMessageText:messageText];
	[alert setAlertStyle:NSWarningAlertStyle];
	[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:nil]; 
}

- (IBAction)getURLresults:(id)sender
{
	[spinProgressIndicator startAnimation:self];
	[urlTextField selectText:self];
	[urlTextField setEnabled:NO];
	[submitButton setEnabled:NO];
	[submitButton setHidden:YES];
	[cancelButton setFrame:NSMakeRect([submitButton frame].origin.x, [submitButton frame].origin.y,
			[cancelButton frame].size.width, [cancelButton frame].size.height)];
	[cancelButton setHidden:NO];
	[cancelButton setEnabled:YES];
	
	NSLog(@"* url: %@", [urlTextField stringValue]);
	[urlUtil loadURLfromString:[urlTextField stringValue] target:self selector:@selector(lookupComplete:context:)];
}

- (void)resetControls
{
	[spinProgressIndicator stopAnimation:self];
	[cancelButton setEnabled:NO];
	[cancelButton setHidden:YES];
	[cancelButton display];
	[submitButton setHidden:NO];
	[submitButton setEnabled:YES];

	[urlTextField setEnabled:YES];
	[urlTextField selectText:self];
}

- (IBAction)cancelURLget:(id)sender
{
	if (urlUtil != nil) {
		[urlUtil cancelLoad];
		[resultsTextView replaceCharactersInRange:NSMakeRange(0, [[resultsTextView string] length])
					withString:@"<<< NO ERROR: User Canceled >>>"];
	}
	[self resetControls];
}

- (void)lookupComplete:(NSData *)data context:(NSDictionary *)context
{
	NSLog(@"Complete!");
	if (data) {
		int  statusCode = 0;
		NSURLResponse *response = [context objectForKey:@"response"];
		if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
			statusCode = [(NSHTTPURLResponse *)response statusCode];
			NSLog(@"HTTP: %@", [NSString stringWithFormat:@"Status: %i", statusCode]);
		}
		if (statusCode < 400) {
		    NSLog(@"( No error! )");
			[resultsTextView replaceCharactersInRange:NSMakeRange(0, [[resultsTextView string] length])
					withString:[[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding]];
		}
	} else {
		NSLog(@"*Error: %@", [(NSError *)[context objectForKey:@"error"] localizedDescription]);
		[resultsTextView replaceCharactersInRange:NSMakeRange(0, [[resultsTextView string] length])
					withString:[NSString stringWithFormat:@"<<< ERROR: %@ >>>",
					[(NSError *)[context objectForKey:@"error"] localizedDescription]]];
	}
	[self resetControls];
}

@end
